package com.example.webviewexample;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.TextView;

import com.example.webviewexample.provider.HtmlFileContentProvider;

public class WebViewMainActivity extends Activity implements OnClickListener,
        WebViewController {
    private static final String DEFAULT_WEBSITE_URL = "http://www.baidu.com";
    private WebView mWebView;
    private ExampleWebViewClient mWebViewClient;
    private ExampleWebChromeClient mWebChromeClient;
    private EditText mUrlEdit;
    private TextView mActionGo;

    private String mUrlInput = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        String destUri = DEFAULT_WEBSITE_URL;

        if (savedInstanceState != null) {
            mWebView.restoreState(savedInstanceState);
        } else {
            Intent intent = getIntent();
            Uri uri = intent.getData();
            if (uri != null) {
                destUri = "file".equals(uri.getScheme()) ? HtmlFileContentProvider.BASE_URI
                        + uri.getEncodedPath()
                        : uri.toString();
            }
        }

        loadUrl(destUri);
    }

    private void initViews() {
        mUrlEdit = (EditText) findViewById(R.id.url_input);
        mActionGo = (TextView) findViewById(R.id.action_go);
        mActionGo.setClickable(true);
        mActionGo.setOnClickListener(this);

        initWebView();
    }

    private void initWebView() {
        mWebView = (WebView) findViewById(R.id.webview);
        if (mWebView != null) {
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setSupportZoom(true);
            webSettings.setDefaultTextEncodingName("utf-8");

            mWebView.requestFocus();

            mWebViewClient = new ExampleWebViewClient(WebViewMainActivity.this);
            mWebView.setWebViewClient(mWebViewClient);

            mWebChromeClient = new ExampleWebChromeClient(
                    WebViewMainActivity.this);
            mWebView.setWebChromeClient(mWebChromeClient);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView != null
                && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.options_view_source:
            viewSource();
            break;
        }
        return true;
    }

    private void viewSource() {
        String url = getInputUrl();
        if (!TextUtils.isEmpty(url)) {
            Intent intent = new Intent();
            intent.setClass(WebViewMainActivity.this, ViewSourceActivity.class);
            intent.putExtra(WebViewExampleConstant.EXTRA_VIEW_SOURCE_URL, url);
            startActivity(intent);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        mWebView.saveState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.action_go:
            String url = getInputUrl();
            performGotoUrl(url);
            break;
        }
    }

    private String getInputUrl() {
        return mUrlEdit.getText().toString();
    }

    private void performGotoUrl(String url) {
        if (url != null && !"".equals(url.trim())) {
            mUrlInput = url;

            if (mWebView != null) {
                loadUrl(mUrlInput);
            }
        }
    }

    @Override
    public void loadUrl(String url) {
        if (mWebView != null) {
            mWebView.loadUrl(url);
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (mUrlEdit != null) {
            mUrlEdit.setText(url);
        }
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        setTitle(title);
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100);
    }

    @Override
    public void setJavaScriptInterface(JavaScriptLocalObject obj,
            String interfaceName) {

    }
}
